var resObject;
var foregroundColorWindow;
var mode; //0:ActiveX1 (IE7) / 1:ActiveX2 / 2:XMLHttpRequest (Firefox)

function init()
{
	try
	{
		resObject = new ActiveXObject("Microsoft.XMLHTTP");
		mode = 0;
	}
	catch(Error)
	{
		try
		{
			resObject = new ActiveXObject("MSXML2.XMLHTTP");
			mode = 1;
		}
		catch(Error)
		{
			try
			{
				resObject = new XMLHttpRequest();
				mode = 2;
			}
			catch(Error)
			{
				alert("Could not initialise the XMLHttpRequest-Object.");
			}
		}
	}
}

function changeColorRGB()
{
	if(mode==2)
	{
		red = this.document.getElementById('red').value;
		green = this.document.getElementById('green').value;
		blue = this.document.getElementById('blue').value;

		error = false;

		if(red < 0)
		{
			alert("The input must be between 0 and 255.");
			red = 0;
			error = true;
		}
		if(red > 255)
		{
			alert("The input must be between 0 and 255.");
			red = 255;
			error = true;
		}
		if(green < 0)
		{
			alert("The input must be between 0 and 255.");
			green = 0;
			error = true;
		}
		if(green > 255)
		{
			alert("The input must be between 0 and 255.");
			green = 255;
			error = true;
		}
		if(blue < 0)
		{
			alert("The input must be between 0 and 255.");
			blue = 0;
			error = true;
		}
		if(blue > 255)
		{
			alert("The input must be between 0 and 255.");
			blue = 255;
			error = true;
		}

		if(red!=parseInt(red) && red.length > 0)
		{
			alert("Unexpected datatype. Pleanse enter an integer.");
			red = 0;
			error = true;
		}
		if(green!=parseInt(green) && green.length > 0)
		{
			alert("Unexpected datatype. Pleanse enter an integer.");
			green = 0;
			error = true;
		}
		if(blue!=parseInt(blue) && blue.length > 0)
		{
			alert("Unexpected datatype. Pleanse enter an integer.");
			blue = 0;
			error = true;
		}

		if(error)
		{
			this.document.getElementById('red').value = red;
			this.document.getElementById('green').value = green;
			this.document.getElementById('blue').value = blue;
		}

		this.document.getElementById('colorpreview').style.background = "#"+getHex(red)+getHex(green)+getHex(blue);
		this.document.getElementById('hex').value = getHex(red)+getHex(green)+getHex(blue);
	}
	else
	{
		redVal = red.value;
		greenVal = green.value;
		blueVal = blue.value;

		error = false;

		if(redVal < 0)
		{
			alert("The input must be between 0 and 255.");
			redVal = 0;
			error = true;
		}
		if(redVal > 255)
		{
			alert("The input must be between 0 and 255.");
			redVal = 255;
			error = true;
		}
		if(greenVal < 0)
		{
			alert("The input must be between 0 and 255.");
			greenVal = 0;
			error = true;
		}
		if(greenVal > 255)
		{
			alert("The input must be between 0 and 255.");
			greenVal = 255;
			error = true;
		}
		if(blueVal < 0)
		{
			alert("The input must be between 0 and 255.");
			blueVal = 0;
			error = true;
		}
		if(blueVal > 255)
		{
			alert("The input must be between 0 and 255.");
			blueVal = 255;
			error = true;
		}

		if(redVal!=parseInt(redVal) && redVal.length > 0)
		{
			alert("Unexpected datatype. Pleanse enter an integer.");
			redVal = 0;
			error = true;
		}
		if(greenVal!=parseInt(greenVal) && greenVal.length > 0)
		{
			alert("Unexpected datatype. Pleanse enter an integer.");
			greenVal = 0;
			error = true;
		}
		if(blueVal!=parseInt(blueVal) && blueVal.length > 0)
		{
			alert("Unexpected datatype. Pleanse enter an integer.");
			blueVal = 0;
			error = true;
		}

		if(error)
		{
			red.value = redVal;
			green.value = greenVal;
			blue.value = blueVal;
		}

		colorpreview.style.background = "#"+getHex(redVal)+getHex(greenVal)+getHex(blueVal);
		hex.value = getHex(redVal)+getHex(greenVal)+getHex(blueVal);
	}
}

function changeColorHex()
{
	if(mode==2)
	{
		hex = this.document.getElementById('hex').value;

		if(hex.charAt(0)=='#')
		{
			hex = hex.substring(1, hex.length);
		}

		red = parseInt(hex.charAt(0)+hex.charAt(1), 16);
		green = parseInt(hex.charAt(2)+hex.charAt(3), 16);
		blue = parseInt(hex.charAt(4)+hex.charAt(5), 16);
		
		if(hex.length>=2)
			this.document.getElementById('red').value = red;
		if(hex.length>=4)
			this.document.getElementById('green').value = green;
		if(hex.length>=6)
			this.document.getElementById('blue').value = blue;

		this.document.getElementById('colorpreview').style.background = "#"+getHex(red)+getHex(green)+getHex(blue);
		this.document.getElementById('hex').value = hex;
	}
	else
	{
		hexVal = hex.value;

		if(hexVal.charAt(0)=='#')
		{
			hexVal = hexVal.substring(1, hexVal.length);
		}

		redVal = parseInt(hexVal.charAt(0)+hexVal.charAt(1), 16);
		greenVal = parseInt(hexVal.charAt(2)+hexVal.charAt(3), 16);
		blueVal = parseInt(hexVal.charAt(4)+hexVal.charAt(5), 16);
		
		if(hexVal.length>=2)
			red.value = redVal;
		if(hexVal.length>=4)
			green.value = greenVal;
		if(hexVal.length>=6)
			blue.value = blueVal;

		colorpreview.style.background = "#"+getHex(redVal)+getHex(greenVal)+getHex(blueVal);
		hex.value = hexVal;
	}
}


function saveColor()
{
	if(mode==2)
	{
		red = this.document.getElementById('red').value;
		green = this.document.getElementById('green').value;
		blue = this.document.getElementById('blue').value;
		requestSaveColor(red, green, blue);
	}
	else
	{
		redVal = red.value;
		greenVal = green.value;
		blueVal = blue.value;
		requestSaveColor(redVal, greenVal, blueVal);
	}
}

function requestSaveColor(red, green, blue)
{
	resObject.open('get', './savecolor.php?red='+red+'&green='+green+'&blue='+blue, true)
	resObject.onreadystatechange = responseSaveColor;
	resObject.send(null);
}

function responseSaveColor()
{
	if(opener != null)
		opener.location.reload(true);

	self.close();
}

function getHex(number)
{
	chr = new Array(16);

	chr[0] = '0';
	chr[1] = '1';
	chr[2] = '2';
	chr[3] = '3';
	chr[4] = '4';
	chr[5] = '5';
	chr[6] = '6';
	chr[7] = '7';
	chr[8] = '8';
	chr[9] = '9';
	chr[10] = 'A';
	chr[11] = 'B';
	chr[12] = 'C';
	chr[13] = 'D';
	chr[14] = 'E';
	chr[15] = 'F';

	left = 0;
	right = 0;

	while((16*left+right) < number)
	{
		right++;
		if(right > 15)
		{
			left++;
			right = 0;
		}
	}

	return chr[left]+chr[right];
}





